--
-- Author: shunguo.chen
-- Date: 2017-01-10 11:06:25
--
-- HeadMenuView

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.HeadMenu

function ClassRef:init()
	display.nodeRemoveForNotHit(self._root, function() 
	        if self:isVisible() then
	        	executeFunc(self._onHideView)
	        end
	    end)

	local btn = self._ui.Button_1
	self._btnHeight = btn:getContentSize().height
	self._btns = {btn}
	self._texts = {self._ui.Text_1}

	self._arrowSize = self._ui.arrow:getContentSize()
end

function ClassRef:show()
	self.super.show(self)
end

function ClassRef:getType()
	return self._type
end

function ClassRef:refresh(data)
	self._data = data
	self._type = data.type
	self._attr = data.attr
	self._funcs = data.funcs
	self._online = data.online
	self._userInfo = data.userInfo
	
	self:refreshBtns()
	self:refreshPlayer()
	self:refreshPos(data.pos)
end

function ClassRef:refreshBtns()
	local size = self._ui.Panel:getContentSize()
	local height = #self._funcs * (self._btnHeight + 10) + 20
	self._ui.Panel:setContentSize(cc.size(size.width, height))
	self._ui.bg:setContentSize(cc.size(size.width, height))
	self._root:setContentSize(cc.size(size.width, height))
	ccui.Helper:doLayout(self._root)

	for i,btn in ipairs(self._btns) do
		btn:setVisible(false)
		self._texts[i]:setVisible(false)
	end

	local initPosY = height - 15 - self._btnHeight/2
	for i,func in ipairs(self._funcs) do
		local btn = self._btns[i]
		local text = self._texts[i]
		if not btn then
			btn = self._btns[1]:clone()
			self._ui.Panel:addChild(btn)
			self._btns[#self._btns + 1] = btn

			text = self._texts[1]:clone()
			self._ui.Panel:addChild(text)
			self._texts[#self._texts + 1] = text
		end
		local posY = initPosY - (i-1)*(self._btnHeight+10)
		btn:setPositionY(posY)
		text:setPositionY(posY)

		btn:setVisible(true)
		text:setVisible(true)

		text:setString(func.name)
		if func.showRedPoint then
			btn:findChild("dot"):show()
		else
			btn:findChild("dot"):hide()
		end

		btn:addClickEventListener(function()
			executeFunc(self._onOperate, func.key, self._data)
		end)
	end
end

function ClassRef:refreshPlayer()
	-- local iconPath = GD:getClassIconPath(self._attr.classId, 2)
 --    if iconPath then
 --        self._ui.zhiye:setVisible(true)
 --        self._ui.zhiye:loadTexture(iconPath)
 --    else
 --        self._ui.zhiye:setVisible(false)
 --    end
 --    self._ui["Bg_Name/05name"]:setString(self._attr.name)
 --    self._ui["04Lv/02Num"]:setString(self._attr.level)

 --    local con = self._ui.PlayerIcon
 --    local circleNode = con:getChildByName("bg_y")
 --    con:reorderChild(circleNode, 1000)

 --    -- 到这里的equips，
 --    -- 1.有可能是地图上过来的equips（{id=, recastTimes=}）
 --    -- 2.有可能是Equip对象，需要处理一下
 --    local avatarEquips = {}
 --    if self._attr.equips and #self._attr.equips > 0 then
	-- 	for _,v in pairs(self._attr.equips) do
	-- 		if iskindof(v, "Equip") then
	-- 			table.insert(avatarEquips, {id = v.tid, recastTimes = v.recastTimes})
	-- 		elseif v.id then
	-- 			table.insert(avatarEquips, v)
	-- 		end
	-- 	end
	-- end

	-- self._avatarIconNode:update(self._attr)
end

function ClassRef:refreshPos(pos)
	local minY = self._arrowSize.height/2 + 2
	local perfectY = 40
	local size = self._ui.Panel:getContentSize()
	local halfHei = size.height/2
	local halfWinHei = display.height/2

	-- posY
	local posY
	local arrowPosY
	local isUp = pos.y > halfWinHei
	local midDis = math.abs(pos.y - halfWinHei)
	if midDis <= (halfHei-perfectY) then
		posY = halfWinHei
		arrowPosY = isUp and (midDis + halfHei) or (halfHei - midDis)
	elseif midDis > (halfHei - perfectY) and midDis <= (halfWinHei - perfectY) then
		posY = isUp and (pos.y - halfHei + perfectY) or (pos.y + halfHei - perfectY)
		arrowPosY = isUp and (size.height - perfectY) or perfectY
	else
		posY = isUp and (display.height - halfHei) or halfHei
		local min = math.max(halfWinHei - midDis, minY)
		arrowPosY = isUp and (size.height - min) or min
	end

	-- posX
	local width = size.width + 2 * self._arrowSize.width
	local isLeft = (pos.x + width) > display.width
	local posX = isLeft and (pos.x - width) or pos.x
	local arrow = isLeft and self._ui.arrow_right or self._ui.arrow

	self._ui.arrow_right:setVisible(isLeft)
	self._ui.arrow:setVisible(not isLeft)

	self:setVisible(true)

	self:setPosition(posX - display.safe_x, posY)
	arrow:setPositionY(arrowPosY)
end

return ClassRef